import { Button, Input, message, Modal, Select } from 'antd';
import { connect } from 'dva';
import React from 'react';
import styles from './index.less';

const { Option } = Select;

@connect(state => ({}))
export default class AddModal extends React.PureComponent {
  state = {
    // tenantCode: undefined,
    dataPermissionType: undefined,
    dataPermissionItemLevel: '',
    dataPermissionItemValue: '',
    submitBntLading: false,
  };

  componentDidMount() {}

  handleSubmit = () => {
    const { changeAddModalVisible } = this.props;
    const {
      // tenantCode,
      dataPermissionType,
      dataPermissionItemLevel,
      dataPermissionItemValue,
    } = this.state;

    let submitData = {};
    /* if (!tenantCode) {
      message.error('租户不能为空');
      return;
    } else {
      submitData.tenantCode = tenantCode;
    } */
    if (!dataPermissionType.trim()) {
      message.error('数据权限类型不能为空');
      return;
    } else {
      submitData.dataPermissionType = dataPermissionType;
    }
    if (!dataPermissionItemLevel.trim()) {
      message.error('数据权限标签不能为空');
      return;
    } else {
      submitData.dataPermissionItemLevel = dataPermissionItemLevel;
    }
    if (!dataPermissionItemValue.trim()) {
      message.error('数据权限值不能为空');
      return;
    } else {
      submitData.dataPermissionItemValue = dataPermissionItemValue;
    }

    this.setState({
      submitBntLading: true,
    });
    this.props.dispatch({
      type: 'http/postObjWithRes',
      payload: {
        url: '/datapermission/config/add2',
        ...submitData,
      },
      callback: response => {
        if (response && response.statusCode == '0') {
          // console.log('success', response);
          message.success('操作成功');
          changeAddModalVisible(true);
        } else {
          // message.error(response.statusMessage);
        }
        this.setState({
          submitBntLading: false,
        });
      },
    });
  };

  handleRadioClick = e => {
    this.setState({
      radioValue: e.target.value,
    });
  };

  render() {
    const {
      submitBntLading,
      tenantCode,
      dataPermissionType,
      dataPermissionItemLevel,
      dataPermissionItemValue,
    } = this.state;

    const { changeAddModalVisible, tenantList, permissionTypeList } = this.props;

    return (
      <Modal
        title="新增租户权限配置"
        onCancel={() => changeAddModalVisible(false)}
        visible={true}
        footer={[
          <Button key="back" type="ghost" onClick={() => changeAddModalVisible(false)}>
            取 消
          </Button>,
          <Button
            key="submit"
            type="primary"
            loading={submitBntLading}
            onClick={() => this.handleSubmit()}
          >
            提 交
          </Button>,
        ]}
      >
        <div className={styles.addModal}>
          {/* <div className={styles.publicItem}>
            <div className={styles.itemLeft}>
              <span>*</span>租户 :
            </div>
            <div className={styles.itemRight}>
              <Select
                value={tenantCode}
                placeholder="请选择租户"
                onChange={e => {
                  this.setState({
                    tenantCode: e,
                  });
                }}
              >
                {tenantList.map(tenant => {
                  return (
                    <Option key={tenant.sid} value={tenant.tenantCode}>
                      {tenant.tenantName}
                    </Option>
                  );
                })}
              </Select>
            </div>
          </div> */}
          <div className={styles.publicItem}>
            <div className={styles.itemLeft}>
              <span>*</span>数据权限类型 :
            </div>
            <div className={styles.itemRight}>
              <Select
                value={dataPermissionType}
                placeholder="请选择数据权限类型"
                onChange={e => {
                  this.setState({
                    dataPermissionType: e,
                  });
                }}
              >
                {permissionTypeList.map(permission => {
                  return (
                    <Option key={permission.dictKey} value={permission.dictKey}>
                      {permission.dictValue}
                    </Option>
                  );
                })}
              </Select>
            </div>
          </div>
          <div className={styles.publicItem}>
            <div className={styles.itemLeft}>
              <span>*</span>数据权限标签 :
            </div>
            <div className={styles.itemRight}>
              <Input
                maxLength={200}
                value={dataPermissionItemLevel}
                placeholder={'请输入数据权限标签'}
                onChange={e => {
                  this.setState({
                    dataPermissionItemLevel: e.target.value || '',
                  });
                }}
              />
            </div>
          </div>
          <div className={styles.publicItem}>
            <div className={styles.itemLeft}>
              <span>*</span>数据权限值 :
            </div>
            <div className={styles.itemRight}>
              <Input
                value={dataPermissionItemValue}
                placeholder={'请输入数据权限值'}
                onChange={e => {
                  this.setState({
                    dataPermissionItemValue: e.target.value || '',
                  });
                }}
              />
            </div>
          </div>
        </div>
      </Modal>
    );
  }
}
